package com;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogFind {
	
	public static final String LINE_SEPARATOR = "\n";
	
	
    
	public static void main(String[] args) throws Exception {
		File file = new File("/home/xc_chat_logs");
		Map<String, Set<Long>> map = new TreeMap<>(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				String o1t = o1.replaceAll("-", "");
				String o2t = o2.replaceAll("-", "");
				return Integer.parseInt(o1t) - Integer.parseInt(o2t);
			}
			
		});
		Map<String, Integer> mapC = new TreeMap<>(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				String o1t = o1.replaceAll("-", "");
				String o2t = o2.replaceAll("-", "");
				return Integer.parseInt(o1t) - Integer.parseInt(o2t);
			}
			
		});
		if(file.exists()) {
			File[] files = file.listFiles();
			if(files == null || files.length == 0) {
				return;
			}else {
				for(File file2 : files) {
					if(file2 != null && file2.isDirectory()) {
						for(File file3 : file2.listFiles()) {
							List<String> readLines = readLines(file3, null);
							for(String readLine : readLines) {
								String tn = file3.getName().substring(file3.getName().lastIndexOf('.'));
								Set<Long> set = map.get(tn);
								if(set != null && set.size() > 0) {
								}else {
									set = new HashSet<Long>();
									map.put(tn, set);
								}
								Integer integerc = mapC.get(tn);
								if(integerc == null) {
									integerc = 0;
								}
								set = map.get(file3.getName().substring(file3.getName().lastIndexOf('.')));
								String regex = "(?<=用户:)\\d*";
								
								Matcher m1= Pattern.compile(regex).matcher(readLine);
								String group = null;
								if(m1.find()) {
									group = m1.group(0);
									integerc++;
									mapC.put(tn, integerc);
								}else {
									System.out.println(readLine);
								}
								if(group != null) {
									set.add(Long.parseLong(group));
								}
							}
						}
					}
				}
			}
			
		}
		
		File filer = new File("/home/webadmin/xc_chat_logs_result");
		List<String> lines = new ArrayList<String>();
		for(Map.Entry<String, Set<Long>> me : map.entrySet()) {
			String strr = me.getKey() + "\t" + me.getValue().size();
			lines.add(strr);
		}
		writeLines(filer, "utf-8", lines, false);
		
		File filerc = new File("/home/webadmin/xc_chat_logs_result_counts");
		List<String> linesc = new ArrayList<String>();
		for(Map.Entry<String, Integer> mec : mapC.entrySet()) {
			String strrc = mec.getKey() + "\t" + mec.getValue();
			linesc.add(strrc);
		}
		writeLines(filerc, "utf-8", linesc, false);
		
	}
	
	public static void writeLines(File file, String encoding, Collection<?> lines, boolean append) throws IOException {
        writeLines(file, encoding, lines, null, append);
    }
	
	public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canWrite() == false) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    throw new IOException("Directory '" + parent + "' could not be created");
                }
            }
        }
        return new FileOutputStream(file, append);
    }
	
	public static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding, boolean append)
            throws IOException {
        FileOutputStream out = null;
        try {
            out = openOutputStream(file, append);
            final BufferedOutputStream buffer = new BufferedOutputStream(out);
            writeLines(lines, lineEnding, buffer, encoding);
            buffer.flush();
            out.close(); // don't swallow close Exception if copy completes normally
        } finally {
            closeQuietly(out);
        }
    }
	
	public static void writeLines(Collection<?> lines, String lineEnding,
            OutputStream output, String encoding) throws IOException {
        writeLines(lines, lineEnding, output, Charset.forName(encoding));
    }
	
	 public static void writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding)
	            throws IOException {
	        if (lines == null) {
	            return;
	        }
	        if (lineEnding == null) {
	            lineEnding = LINE_SEPARATOR;
	        }
	        Charset cs = encoding;
	        for (Object line : lines) {
	            if (line != null) {
	                output.write(line.toString().getBytes(cs));
	            }
	            output.write(lineEnding.getBytes(cs));
	        }
	    }
	
	public static List<String> readLines(File file, Charset encoding) throws IOException {
        InputStream in = null;
        try {
            in = openInputStream(file);
            return readLines(in, "utf-8");
        } finally {
            closeQuietly(in);
        }
    }
	
	public static FileInputStream openInputStream(File file) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canRead() == false) {
                throw new IOException("File '" + file + "' cannot be read");
            }
        } else {
            throw new FileNotFoundException("File '" + file + "' does not exist");
        }
        return new FileInputStream(file);
    }
	
	public static List<String> readLines(InputStream input, String encoding) throws IOException {
        InputStreamReader reader = new InputStreamReader(input, encoding);
        return readLines(reader);
    }
	public static List<String> readLines(Reader input) throws IOException {
        BufferedReader reader = toBufferedReader(input);
        List<String> list = new ArrayList<String>();
        String line = reader.readLine();
        while (line != null) {
            list.add(line);
            line = reader.readLine();
        }
        return list;
    }
	
	public static BufferedReader toBufferedReader(Reader reader) {
        return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
    }
	
	public static void closeQuietly(InputStream input) {
        closeQuietly((Closeable)input);
    }
	
	public static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }
}
